#include"sqstack.h"

sqstack* stack_create(void)
{
	sqstack *stack = malloc(sizeof(sqstack));
	if (stack == NULL)
		return NULL;
	stack->top = -1;
	return stack;
}

int stack_push(sqstack* st,datatype * data)
{
	if (st->top == MAXSIZE - 1)
		return -1;
	st->data[++st->top] = *data;
	return 0;
}

int stack_pop(sqstack *st,datatype *data)
{
	if(stack_isempty(st))
		return -1;
	*data = st->data[st->top--];
	return 0;
}

int stack_show(sqstack *st)
{
	if(stack_isempty(st))
		return -1;
	printf("栈底<<  ");
	for(int i = 0;i<=st->top;i++)
		printf("%d  ",st->data[i]);
	printf(">>栈顶\n");
}

void stack_destory(sqstack** st)
{
	free(*st);
	*st = NULL;
}

int stack_isempty(sqstack *st)
{
	return (st->top == -1);
}

int stack_top(sqstack *st,datatype *data)
{
	if (stack_isempty(st))
		return -1;
	*data = st->data[st->top];
	return 0;
}

int PrintMatchedPairs(sqstack *st,char *expression)
{
	int i = 0;
	int tmp;
	while (expression[i] != '\0')
	{
		if (expression[i] == '(')
			stack_push(st, (int *)&expression[i]);
		else if (expression[i] == ')')
		{
			int ret = stack_pop(st, &tmp);
			if (ret == -1)
				return -1;
		}
		i++;
	}
	if (!stack_isempty(st))
		return -1;
	return 0;
}